package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenlida
 * @date 2018/12/20 14:34
 * @description
 */
public class Code260 {
    public int[] singleNumber(int[] nums) {
        if (nums.length < 2) { return nums; }
        Arrays.sort(nums);
        List<Integer> list = new ArrayList<>();
        if (nums[0] != nums[1]) {
            list.add(nums[0]);
        }
        if (nums[nums.length - 1] != nums[nums.length - 2]) {
            list.add(nums[nums.length - 1]);
        }
        for (int i = 1; i < nums.length - 1; i++) {
            if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) {
                list.add(nums[i]);
            }
        }
        int[] result = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
}
